<?php

namespace App\Jobs\Train;

use App\Models\Train\Trains\Repositories\Interfaces\TrainsRepositoryInterface;
use App\Models\Train\Trains\Repositories\TrainsRepository;
use App\Trip\Train\TrainSchedule\TrainSchedule;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessAddTrain implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private TrainSchedule $train;


    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(TrainSchedule $train)
    {
        $this->train = $train;
//        Log::info("add train by queue:".$this->train);
    }

    /**
     * Execute the job.
     *
     * @return void
     * @throws Exception
     */
    public function handle()
    {
        /**
         * @var TrainsRepository $trainRepo
         */
        $trainRepo = app()->make(TrainsRepositoryInterface::class);
        $trainRepo->addTrain($this->train->toArray());
    }
}
